/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * @(#)WaitDialogBox.java Jul 19, 2008
 *
 * Placeholder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Image;

/**
 * Defines a {@link DialogBox} for displaying wait icon and text. It represents
 * some background work is ongoing and the user is not supposed to do anything
 * and wait for the result of the background action.
 * <p>
 * Normally, there should be only one instance of this class, so, providing the
 * static methods to access this single instance.
 */
public class WaitDialogBox extends DialogBox {
	private static WaitDialogBox mInstance;

	/**
	 * Starts the default instance.
	 * 
	 * @param message
	 *            the message to be displayed
	 */
	public static final void start(String message) {
		if (mInstance == null) {
			mInstance = new WaitDialogBox();
			mInstance.setMessage(message);
		}
		int left = Window.getClientWidth() / 2;
		int top = Window.getClientHeight() / 2;
		mInstance.setPopupPosition(left, top);
		mInstance.show();
	}

	/**
	 * Stops the default wait dialog box.
	 */
	public static final void stop() {
		if (mInstance != null) {
			mInstance.hide();
		}
	}

	private static final String LAYOUT_PATTERN = "|@|@{ming-WaitDialogBoxMessage}|";

	private final GridLayout mLayout;

	/**
	 * Initializes a new instance of <code>WaitDialogBox</code>.
	 */
	public WaitDialogBox() {
		mLayout = new GridLayout(LAYOUT_PATTERN);

		this.layoutPanel();
		this.add(mLayout);
	}

	/** */
	private void layoutPanel() {
		this.setStyleName("ming-WaitDialogBox");
		Image image = new Image("images/working.gif");
		mLayout.setWidget(0, image);
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public final void setMessage(final String message) {
		mLayout.setText(1, message);
	}

	/**
	 * @return the message
	 */
	public final String getMessage() {
		return mLayout.getText(1);
	}
}
